package nyist.media.controller;

import com.qiniu.common.Zone;
import com.qiniu.http.Response;
import com.qiniu.storage.BucketManager;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.UploadManager;
import lombok.extern.slf4j.Slf4j;
import nyist.media.exception.BusinessException;
import nyist.media.page.ApiResult;
import nyist.media.service.QiniuService;
import nyist.media.service.impl.QiNiuServiceImpl;
import nyist.media.utils.RSAUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.security.interfaces.RSAPublicKey;
import java.util.*;

/**
 * 公用方法
 *
 * @author dcf
 */
@Controller
@RequestMapping("/common")
@Slf4j
public class CommonController {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private QiniuService qiNiuService;


    @RequestMapping(value = "/logout", method = RequestMethod.GET)
    public String logout() {
        SecurityUtils.getSubject().logout();
        return "redirect:/admin/index";
    }

    /**
     * 公钥
     */
    @RequestMapping(value = "/public_key", method = RequestMethod.GET)
    public @ResponseBody Map<String, String> publicKey(HttpServletRequest request) {
        RSAPublicKey publicKey = RSAUtils.generateKey(stringRedisTemplate, request.getSession().getId());
        Map<String, String> data = new HashMap<>();
        data.put("modulus", Base64.getEncoder().encodeToString(publicKey.getModulus().toByteArray()));
        data.put("exponent", Base64.getEncoder().encodeToString(publicKey.getPublicExponent().toByteArray()));
        return data;
    }

    /**
     * 错误提示
     */
    @RequestMapping("/error")
    public String error() {
        return "/common/error";
    }

    /**
     * 资源不存在
     */
    @RequestMapping("/resource_not_found")
    public String resourceNotFound() {
        return "/common/404";
    }

    /**
     * 权限错误
     */
    @RequestMapping("/unauthorized")
    public String unauthorized(HttpServletRequest request, HttpServletResponse response) {
        String requestType = request.getHeader("X-Requested-With");
        if ("XMLHttpRequest".equalsIgnoreCase(requestType)) {
            response.addHeader("loginStatus", "unauthorized");
            try {
                response.sendError(HttpServletResponse.SC_FORBIDDEN);
            } catch (IOException e) {
                log.error(e.getMessage(), e);
            }
            return null;
        }
        return "/common/unauthorized";
    }
}
